//
//  MYGExpertTaskDetailHeaderView.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/5/30.
//

import UIKit
import QMUIKit

class MYGExpertTaskDetailHeaderView: UIView {

    var weeks:[MYGMeetDateModel] = []
    var didSelectCell:((_ index:Int)->Void)?
    private var model = MYGAcademicExpertModel()
    func getHeaderData(model:MYGAcademicExpertModel){
        self.model = model
        namelabel.text = model.expertCn
        hosTitle.text = model.hospital
        avar.sd_setImage(with: URL(string: model.photo),placeholderImage: UIImage(named: "head"))
        contentTitle.text = model.introduction
        
        for (index,item) in model.task_list.enumerated() {
            var m = MYGMeetDateModel()
            m.week = item.week
            m.day = item.day
            m.year_month = item.year_month
            m.lunar = item.lunar
            m.isSelected = index != 0
            m.date = item.time
            weeks.append(m)
            
        }
        self.subTileView.model = weeks.first ?? MYGMeetDateModel()

        dateCollectionView.reloadData()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    func configUI(){
        backgroundColor = .white
        addSubview(topImageView)
        addSubview(popBackBtn)
        addSubview(titleLabel)
        topImageView.addSubview(avar)
        topImageView.addSubview(namelabel)
        topImageView.addSubview(hosTitle)
        topImageView.addSubview(contentTitle)
        topImageView.addSubview(line)
        topImageView.addSubview(nextButton)
        
        topImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(statusBarHeight + 12)
        }
        popBackBtn.snp.makeConstraints { make in
            make.width.height.equalTo(44)
            make.left.equalTo(18)
            make.centerY.equalTo(titleLabel)
        }
        avar.snp.makeConstraints { make in
            make.width.height.equalTo(50)
            make.left.equalTo(20)
            make.top.equalToSuperview().offset(70 + statusBarHeight)
        }
        namelabel.snp.makeConstraints { make in
            make.top.equalTo(avar)
            make.left.equalTo(avar.snp.right).offset(11)
        }
        hosTitle.snp.makeConstraints { make in
            make.bottom.equalTo(avar)
            make.left.equalTo(namelabel)
        }
        
        nextButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-12)
            make.centerY.equalTo(avar)
        }
        
        contentTitle.snp.makeConstraints { make in
            make.left.equalToSuperview().inset(20)
            make.right.equalToSuperview().inset(10)
            make.top.equalTo(avar.snp.bottom).offset(8)
            make.height.equalTo(100)
        }
  
        topImageView.addSubview(dateCollectionView)
        dateCollectionView.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.left.right.equalToSuperview()
            make.height.equalTo(44)
        }
        
        topImageView.addSubview(subTileView)
        subTileView.snp.makeConstraints { make in
            make.bottom.equalTo(dateCollectionView.snp.top)
            make.left.right.equalToSuperview()
            make.height.equalTo(80)
        }
        subTileView.reserveBtn.isHidden = true
        
        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
            make.top.equalTo(subTileView.snp.top).offset(10)
        }
        
    }
    
    @objc func backAction() {
        UIViewController.currentViewController?.navigationController?.popViewController(animated: true)
    }
    
    @objc func clickNextButton() {
        
        if self.model.doctor_id == 0 {
            return
        }
        let vc = MYGMicrostationViewController()
        var model =  MYGFriendsModel()
        model.id = "\(self.model.id)"
        
        var doctorModel = MYGFriendsDoctorModel()
        
        doctorModel.name = self.model.expertCn
        doctorModel.department = self.model.department
        doctorModel.hospital = self.model.hospital
        doctorModel.id = self.model.doctor_id
        doctorModel.title = self.model.position
        
        model.from_doctor = doctorModel
        
        vc.model = model
        self.rootResponsder().navigationController?.pushViewController(vc, animated: true)
    }
    
    private var titleLabel:UILabel = {
        let control = UILabel()
        control.text = "专家任务详情"
        control.textColor = .rgba(255, 255, 255, 1)
        control.font = .medium(18)
        return control
    }()
    
    lazy var popBackBtn: UIButton = {
        let control = UIButton(type: .custom)
        control.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
        control.tintColor = .white
        control.setImage(UIImage(named: "Chat/back"), for: .normal)
        control.addTarget(self, action: #selector(backAction), for: .touchUpInside)
        return control
    }()
    private lazy var topImageView:UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "Nav bar")
        imageView.isUserInteractionEnabled = true
        return imageView
    }()
    
    private lazy var avar:UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "head")
        imageView.layer.cornerRadius = 25
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    private var namelabel:UILabel = {
        let control = UILabel()
        control.text = "阿辰辰"
        control.textColor = .rgba(255, 255, 255, 1)
        control.font = .medium(16)
        return control
    }()
    
    private  var hosTitle:UILabel = {
        let control = UILabel()
        control.text = "中国医科大学"
        control.font = .regular(13)
        control.textColor = .rgba(255, 255, 255, 1)
        return control
    }()
    
    private  var contentTitle:UITextView = {
        let control = UITextView()
        control.text = "这是专家的简介"
        control.font = .regular(14)
        control.textColor = .rgba(255, 255, 255, 1)
        control.isEditable = false
        control.backgroundColor = .clear
        return control
    }()
    
    lazy var nextButton: QMUIButton = {
        let control = QMUIButton(type: .custom)
        control.setImage(UIImage(named: "MYG_home_moreJ"), for: .normal)
        control.setTitle("前往专家个人主页", for: .normal)
        control.setTitleColor(.white, for: .normal)
        control.titleLabel?.font = .regular(12)
        control.imagePosition = .right
        control.spacingBetweenImageAndTitle = 4
        control.addTarget(self, action: #selector(clickNextButton), for: .touchUpInside)
        return control
    }()
    
    private lazy var line:UIView = {
        let line = UIView()
        line.backgroundColor = .white
        line.alpha = 0.3
        line.isHidden = true
        return line
    }()
    
    var subTileView:MYGAcademicDetailSubtitleView = {
        let control = Bundle.main.loadNibNamed("MYGAcademicDetailSubtitleView", owner: nil)?.first as! MYGAcademicDetailSubtitleView
        control.frame = CGRect.init(x: 0, y: 0, width: screenWidth, height: 80)
        control.backgroundColor = .clear
        control.reserveBtn.isHidden = true
        control.titleLB.isHidden = true
        control.liveLB.isHidden = true
        control.monthLB.textColor = .white
        control.dateLB.textColor = .white
        control.weekLB.textColor = .white
        control.line.isHidden = true
        return control
    }()
    
    lazy var dateCollectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 11
        var leftMargin = 18.0
        layout.itemSize = CGSize(width: 39, height: 44)
        let control = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        control.register(UINib.init(nibName: "MYGAcademicDetailDateCell", bundle: nil), forCellWithReuseIdentifier: "MYGAcademicDetailDateCell")
        
        control.showsHorizontalScrollIndicator = false
        control.showsVerticalScrollIndicator = false
        control.backgroundColor = .clear
        control.delegate = self
        control.dataSource = self
        control.contentInset = UIEdgeInsets(top: 0, left: leftMargin, bottom: 0, right: leftMargin)
        return control
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


extension MYGExpertTaskDetailHeaderView:UICollectionViewDataSource,UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let model = self.weeks[indexPath.row]
        self.weeks = self.weeks.map({ obj in
            var result = obj
            result.isSelected = true
            if model.date == obj.date {
                result.isSelected = false
            }
            return result
        })
        self.subTileView.model = model
        self.dateCollectionView.reloadData()
        didSelectCell?(indexPath.row)
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return weeks.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MYGAcademicDetailDateCell", for: indexPath) as! MYGAcademicDetailDateCell
        let model = weeks[indexPath.row]
        cell.model = model
        return cell
       
    }


}
